<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use App\Buy;
use User;
use Auth;
use App\Buyer_info;

class BuyController extends Controller
{
    //利用中间件检测用户登录状态
    //中间件可以注册在控制器上,
    protected $middleware = ['App\Http\Middleware\Authenticate'=>[] ];
    //后台管理首页
    public function index(){
      return view('admin/index');
    }
    //买家列表
    public function maijialist() {
        //
        // $re = DB::table('buys')->get();
        $re = Buy::paginate(4);
        return view('admin/maijialist',['re'=>$re]);
    }

    //买家审核
    public function mjsh(){
      
        return view('admin/maijiashenhe');
    }
    public function buyercheck($bid){
        $mobile = buyer_info::where('bid',$bid)->first();
          $mobile->b_status = 1;
          $mobile->save();
        return view('admin/buyercheck',['mobile'=>$mobile]);
    }

    public function postmjsh(Request $req){
       $rs =  DB::table('buys')->insert([
            'bname'=>$_POST['bname'] , 
            'bmobile'=>$_POST['bmobile'] , 
            'bmoney'=>$_POST['bmoney'],
            'bjobcity'=>$_POST['bjobcity'],
            'pubtime'=>time(),
            ]);
        return $rs ? redirect('mjlb') :'fail';
    }

    public function bdrop($bid){
        $re = DB::table('buys')->where('bid',$bid)->delete();
        return $re ? redirect('mjlb') : 'fail';
    }
    public function beditor(Request $req ,$bid){
        if(empty($_POST)){
            $re = DB::table('buys')->where('bid',$bid)->first();
            return view('admin/beditor',['re'=>$re]);
        }else{
            $res = Buy::find($bid);
            $res->bname = $req->bname;
            $res->bmoney = $req->bmoney;
            $res->bmobile = $req->bmobile;
            $res->bjobcity = $req->bjobcity;
            $res->pubtime = $req->input('pubtime',time());
            return $res->save() ? redirect('mjlb') : 'fail';
        }
    }
    public function reg(){
      return view('admin/reg');
    }

    public function postreg(){
      // $user = new User();
      // $user->name = $req->name;
      // $user->email = $req->email;
      // $user->mobile = $req->mobile;
      // $user->password = $req->password;
      // return $user->save() ? redirect('admin/index') : 'fail';
      $date = [
                   'name'=>$_POST['name'],
                   'password'=>$_POST['password'],
                   'mobile'=>$_POST['mobile'],
                   'email'=>$_POST['email'],
                   'regtime'=>time(),
                   'remember_token'=>$_POST['_token'],
                ];
            $res = DB::table('users')->insert($date);
            return $res ? redirect('adminindex') : 'fail';

    }

}
